package com.mixsmart.web.controller;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mixsmart.commonweb.plugins.IPlugin;
import com.mixsmart.commonweb.plugins.PluginFactory;
import com.mixsmart.core.bean.MixResponse;
import com.mixsmart.core.service.IResQueryService;
import com.mixsmart.utils.StringUtils;
import com.mixsmart.web.plugins.bean.AutoComplete;
import com.mixsmart.web.plugins.bean.ZTreeData;

/**
 * 通用查询控制器类
 * @author lmq
 * @version 1.0
 * @since 1.0
 * 2015年11月6日
 */
@Controller
@RequestMapping("/query")
public class QueryController extends BaseWebController {

	@Autowired
	private IResQueryService resQueryServ;
	@Autowired
	@Qualifier("ztree")
	private IPlugin ztree;
	@Autowired
	@Qualifier("autoComplete")
	private IPlugin autoComplete;
	
	/**
	 * 通过资源名称查询数据；还能指定参数变量的名称paramName和变量值paramValue
	 * @param session HttpSession对象；用于获取存储在session中的用户信息
	 * @param resName 资源名称（SQL名称）
	 * @param paramName 参数变量的名称
	 * @param paramValue 参数变量值
	 * @return 返回MixResponse<Object>对象；以JSON格式方式返回给前台页面；
	 * <p>如果成功查询到数据；则getResult()方法返回值为“1”</p>
	 */
	@RequestMapping("/{resName}.do")
	@ResponseBody
	public MixResponse<Object> query(HttpSession session,@PathVariable String resName,
			String paramName,String paramValue) {
		MixResponse<Object> mixResp = new MixResponse<Object>();
		if(StringUtils.isNotEmpty(resName)) {
			HashMap<String, Object> params = super.getParamMaps(session, paramName, paramValue);
			mixResp = resQueryServ.getDatas(resName, params);
			params = null;
		}
		return mixResp;
	}
	
	/**
	 * 通过资源名称查询数据；传递的参数在request中获取
	 * @param request HttpServletRequest对象，用户获取传递参数及session中的用户信息
	 * @param resName 资源名称（SQL名称）
	 * @return 返回MixResponse<Object>对象；以JSON格式方式返回给前台页面；
	 * <p>如果成功查询到数据；则getResult()方法返回值为“1”</p>
	 */
	@RequestMapping("/req/{resName}.do")
	@ResponseBody
	public MixResponse<Object> query(HttpServletRequest request,@PathVariable String resName) {
		MixResponse<Object> mixResp = new MixResponse<Object>();
		if(StringUtils.isNotEmpty(resName)) {
			HashMap<String, Object> params = super.getRequestParamMap(request);
			mixResp = resQueryServ.getDatas(resName, params);
			params = null;
		}
		return mixResp;
	}
	
	/**
	 *  通过资源名称分页查询数据
	 * @param request HttpServletRequest对象，用户获取传递参数及session中的用户信息
	 * @param resName 资源名称（SQL名称）
	 * @param page 当前查询到的页面数
	 * @return 返回MixResponse<Object>对象，以JSON格式方式返回给前台页面；
	 * <p>如果成功查询到数据；则getResult()方法返回值为“1”</p>
	 */
	@RequestMapping("/page/{resName}.do")
	@ResponseBody
	public MixResponse<Object> queryPage(HttpServletRequest request,@PathVariable String resName,Integer page) {
		MixResponse<Object> mixResp = new MixResponse<Object>();
		page = null == page?1:page;
		page = super.getPage(page);
		if(StringUtils.isNotEmpty(resName)) {
			HashMap<String, Object> params = getRequestParamMap(request);
			mixResp = resQueryServ.getDatas(resName, params,page,super.getPerPageSize());
			params = null;
		}
		return mixResp;
	}
	
	/**
	 * 通过资源名称查询获取zTree插件的数据结构
	 * @param request HttpServletRequest对象
	 * @param resName 资源名称（SQL名称）
	 * @return 返回MixResponse<Object>对象，以JSON格式方式返回给前台页面；
	 * <p>如果成功查询到数据；则getResult()方法返回值为“1”</p>
	 */
	@Deprecated
	@RequestMapping("/ztree/{resName}.do")
	@ResponseBody
	public MixResponse<ZTreeData> zTree(HttpServletRequest request,@PathVariable String resName) {
		MixResponse<ZTreeData> mixResp = new MixResponse<ZTreeData>();
		if(StringUtils.isNotEmpty(resName)) {
			HashMap<String, Object> params = getRequestParamMap(request);
			mixResp = PluginFactory.getPluginDatas(ztree, resName, params);
			params = null;
		}
		return mixResp;
	}
	
	/**
	 * 通过资源名称异步查询获取zTree插件的数据结构
	 * @param request HttpServletRequest对象
	 * @param resName 资源名称（SQL名称）
	 * @return 返回MixResponse<Object>对象，以JSON格式方式返回给前台页面；
	 * <p>如果成功查询到数据；则getResult()方法返回值为“1”</p>
	 */
	@Deprecated
	@RequestMapping("/ztreeAsync/{resName}.do")
	@ResponseBody
	public MixResponse<ZTreeData> zTreeAsync(HttpServletRequest request,@PathVariable String resName) {
		MixResponse<ZTreeData> mixResp = new MixResponse<ZTreeData>();
		if(StringUtils.isNotEmpty(resName)) {
			HashMap<String, Object> params = getRequestParamMap(request);
			params.put("isAsync", true);
			Object obj = params.get("id");
			if(obj.getClass().isArray()) {
				Object[] objs = (Object[])obj;
				params.put("id",objs[objs.length-1]);
			}
			mixResp = PluginFactory.getPluginDatas(ztree, resName, params);
			params = null;
		}
		return mixResp;
	}

	/**
	 *  通过资源名称查询获取AutoComplete插件的数据结构
	 * @param request HttpServletRequest对象
	 * @param resName 资源名称（SQL名称）
	 * @return 返回MixResponse<Object>对象，以JSON格式方式返回给前台页面；
	 * <p>如果成功查询到数据；则getResult()方法返回值为“1”</p>
	 */
	@Deprecated
	@RequestMapping("/autoComplete/{resName}.do")
	@ResponseBody
	public MixResponse<AutoComplete> autoComplete(HttpServletRequest request,@PathVariable String resName) {
		MixResponse<AutoComplete> mixResp = new MixResponse<AutoComplete>();
		if(StringUtils.isNotEmpty(resName)) {
			HashMap<String, Object> params = getRequestParamMap(request);
			mixResp = PluginFactory.getPluginDatas(autoComplete, resName, params);
			params = null;
		}
		return mixResp;
	}
	
}
